package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsGroupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.pojo.DtsTopic;
import com.qf.service.GrouponService;
import com.qf.service.TopicService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/groupon")
@CrossOrigin
public class GrouponController {
    @Autowired
    private GrouponService grouponService;

    //http://localhost:8083/admin/coupon/list?page=1&limit=20&name=rr&type=1&status=2&sort=add_time&order=desc
    @RequestMapping("/list")
    public Object findCoupon(int page,int limit,String sort,String order,Integer goodsId){
        IPage<DtsGrouponRules> dtsGrouponIPage = grouponService.selectPage(page, limit, sort, order,goodsId);
        Map<String,Object> result =new HashMap<>();

        result.put("total",dtsGrouponIPage.getTotal());
        result.put("items",dtsGrouponIPage.getRecords());
        return ResponseUtil.ok(result);
    }

    @PostMapping("/create")
    public Object creat(@RequestBody String body ) throws ParseException {
        Integer discount = JacksonUtil.parseInteger(body, "discount");
        Integer discountMember = JacksonUtil.parseInteger(body, "discountMember");
        String expireTime = JacksonUtil.parseString(body, "expireTime");
        Integer goodsId = JacksonUtil.parseInteger(body, "goodsId");

        DtsGrouponRules grouponRules=new DtsGrouponRules();
        grouponRules.setDiscount(new BigDecimal(discount));
        grouponRules.setDiscountMember(discountMember);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(expireTime);
        grouponRules.setExpireTime(parse);
        grouponRules.setGoodsId(new Long(goodsId));
        grouponService.creat(grouponRules);

        Map<String,Object> result =new HashMap<>();
        result.put("id",grouponRules.getId());
        result.put("goodsId",grouponRules.getGoodsId());
        result.put("goodsName",grouponRules.getGoodsName());
        result.put("discount",grouponRules.getDiscount());
        result.put("discountMember",grouponRules.getDiscountMember());
        result.put("expireTime",grouponRules.getExpireTime());
        result.put("addTime",grouponRules.getAddTime());
        result.put("picUrl",grouponRules.getPicUrl());
        return ResponseUtil.ok(result);
    }
    @PostMapping("/update")
    public Object update(@RequestBody String body ) throws ParseException {
        Integer discount = JacksonUtil.parseInteger(body, "discount");
        Integer discountMember = JacksonUtil.parseInteger(body, "discountMember");
        String expireTime = JacksonUtil.parseString(body, "expireTime");
        Integer goodsId = JacksonUtil.parseInteger(body, "goodsId");

        DtsGrouponRules grouponRules=new DtsGrouponRules();
        grouponRules.setDiscount(new BigDecimal(discount));
        grouponRules.setDiscountMember(discountMember);
        grouponRules.setGoodsId(new Long(goodsId));
        grouponService.update(grouponRules);

        Map<String,Object> result =new HashMap<>();
        result.put("goodsId",grouponRules.getGoodsId());
        result.put("goodsName",grouponRules.getGoodsName());
        result.put("discount",grouponRules.getDiscount());
        result.put("discountMember",grouponRules.getDiscountMember());
        result.put("expireTime",grouponRules.getExpireTime());
        result.put("picUrl",grouponRules.getPicUrl());
        return ResponseUtil.ok(result);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody String body ) throws ParseException {
        Integer discount = JacksonUtil.parseInteger(body, "discount");
        Integer discountMember = JacksonUtil.parseInteger(body, "discountMember");
        String expireTime = JacksonUtil.parseString(body, "expireTime");
        Integer goodsId = JacksonUtil.parseInteger(body, "goodsId");
        DtsGrouponRules grouponRules=new DtsGrouponRules();
        grouponRules.setDiscount(new BigDecimal(discount));
        grouponRules.setDiscountMember(discountMember);
        grouponRules.setGoodsId(new Long(goodsId));
        grouponService.delete(grouponRules);
        return ResponseUtil.ok();
    }

}
